package net.onecloud.onecloud.employ;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import net.onecloud.onecloud.R;
import net.onecloud.onecloud.adapter.PostAdapter;
import net.onecloud.onecloud.application.App;
import net.onecloud.onecloud.base.BaseAppCompatActivity;
import net.onecloud.onecloud.model.Record;
import net.onecloud.onecloud.model.ResultEmploy;
import net.onecloud.onecloud.model.ResultPost;
import net.onecloud.onecloud.model.ResultRecord;
import net.onecloud.onecloud.model.ResultResume;
import net.onecloud.onecloud.model.ResultToemploy;
import net.onecloud.onecloud.model.Toemploy;
import net.onecloud.onecloud.network.NetWork;
import net.onecloud.onecloud.utils.DateUtils;
import net.onecloud.onecloud.utils.LogUtils;
import net.onecloud.onecloud.utils.RecycleViewDivider;
import net.onecloud.onecloud.utils.T;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subscriptions.Subscriptions;

public class PostDetailListPage extends BaseAppCompatActivity implements BaseQuickAdapter.OnRecyclerViewItemChildClickListener {

    private RecyclerView mRecyclerView;
    private PostAdapter mPostAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private ResultPost.DataEntity selectPost;
    private ResultResume.DataEntity selectResume;
    private ResultEmploy.DataEntity employ;
    private int mOnclickPostion = -1;

    Observer<ResultRecord> addRecordObserver = new Observer<ResultRecord>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(ResultRecord resultRecord) {
            LogUtils.i("添加记录成功");
            App.getRecordList().add(resultRecord.getData().get(0));
            if (mOnclickPostion != -1) {
                mPostAdapter.notifyItemChanged(mOnclickPostion);
            }

        }
    };


    Observer<ResultPost> updatePostObserver = new Observer<ResultPost>() {
        @Override
        public void onCompleted() {
            LogUtils.d("updatePostObserver onCompleted");
        }

        @Override
        public void onError(Throwable e) {
            LogUtils.e(e);
        }

        @Override
        public void onNext(ResultPost resultPost) {
            mPostAdapter.setNewData(resultPost.getData());
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_detail_list_page);
        initView();
        initListener();
        initData();
    }

    private void initData() {
        setToolBarTitle("岗位详情");
        HideLeftButton(false);
        employ = (ResultEmploy.DataEntity) getIntent().getExtras().get(EmployPage.POST);
        if (employ != null && !employ.getCampany().equals("")) {
            if (App.getUser() != null) {
                NetWork.getRecordApi().query1(App.getUser().getId())
                        .subscribeOn(Schedulers.io())
                        .flatMap(new Func1<ResultRecord, Observable<ResultPost>>() {
                            @Override
                            public Observable<ResultPost> call(ResultRecord resultRecord) {
                                if (resultRecord.getSuccess() == 1) {
                                    App.setRecordList(resultRecord.getData());
                                }
                                return NetWork.getPostApi().queryPostList(employ.getId());
                            }
                        })
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(updatePostObserver);

            } else {
                NetWork.getPostApi().queryPostList(employ.getId())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(updatePostObserver);
            }

        }


    }

    private void initListener() {
        mPostAdapter.setOnRecyclerViewItemChildClickListener(this);
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.m_recyclerview);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL));
        mPostAdapter = new PostAdapter();
        View emptyView = View.inflate(this, R.layout.empty_view, null);
        mPostAdapter.setEmptyView(emptyView);
        mRecyclerView.setAdapter(mPostAdapter);


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.m_sumbit_resume: {

                submitResume();
                break;
            }
        }
    }

    private void submitResume() {

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
        mOnclickPostion = i;
        if (App.getUser() == null) {
            showHit("您还没有登录哦!");
            return;
        }

        selectPost = (ResultPost.DataEntity) baseQuickAdapter.getItem(i);

        Intent intent = new Intent(PostDetailListPage.this, SelectResumeActivity.class);
        startActivityForResult(intent, 0);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0 && resultCode == RESULT_OK) {
            selectResume = data.getExtras().getParcelable("resume");
            Toemploy toemploy = getToemploy(selectResume);
            NetWork.getToemployApi()
                    .add(toemploy)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .flatMap(new Func1<ResultToemploy, Observable<ResultRecord>>() {
                        @Override
                        public Observable<ResultRecord> call(ResultToemploy resultToemploy) {
                            if (resultToemploy.getSuccess() == 1) {
                                showHit("投递成功");
                                Record record = new Record();
                                record.setRcampany(employ.getCampany());
                                record.setRdate(DateUtils.getCurrentDate2());
                                record.setRpost(selectPost.getPname());
                                record.setUser_id(App.getUser().getId());
                                record.setPost_id(selectPost.getId());
                                return NetWork.getRecordApi()
                                        .addRecord(record)
                                        .subscribeOn(Schedulers.io());
                            }
                            return null;
                        }
                    }).subscribe(addRecordObserver);
        }
    }

    private Toemploy getToemploy(ResultResume.DataEntity selectResume) {
        Toemploy toemploy = new Toemploy();
        toemploy.setUser_id(App.getUser().getId());
        toemploy.setTdate(DateUtils.getCurrentDate2());
        toemploy.setContact(selectResume.getContact());
        toemploy.setRname(selectResume.getRname());
        toemploy.setExprience(selectResume.getExprience());
        toemploy.setRposition(selectResume.getRposition());
        toemploy.setSchool(selectResume.getSchool());
        toemploy.setSex(selectResume.getSex());
        toemploy.setSkill(selectResume.getSkill());
        toemploy.setPost_id(selectPost.getId());
        return toemploy;
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

    }


}
